import 'rxjs/add/operator/finally';
import 'rxjs/add/operator/map';
import { mergeMap as _observableMergeMap, catchError as _observableCatch, map } from 'rxjs/operators';
import { Observable, from as _observableFrom, throwError as _observableThrow, of as _observableOf } from 'rxjs';
import { Injectable, Inject, Optional } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { ApiResult } from 'entities';
import { API_BASE_URL } from '@shared/service-proxies/service-proxies';
import { NzTreeNode } from 'ng-zorro-antd';
import { CommonHttpClient } from 'services/common-httpclient';
import { OrganizationUnits } from 'entities/organizationunits';
import { PagedResultDto } from '@shared/component-base';

@Injectable()
export class OrganizationUnitsServiceProxy {
    private http: HttpClient;
    private _commonhttp: CommonHttpClient;
    private baseUrl: string;
    protected jsonParseReviver: ((key: string, value: any) => any) | undefined = undefined;

    constructor(@Inject(HttpClient) http: HttpClient, @Inject(CommonHttpClient) commonhttp: CommonHttpClient, @Optional() @Inject(API_BASE_URL) baseUrl?: string) {
        this.http = http;
        this.baseUrl = baseUrl ? baseUrl : "";
        this._commonhttp = commonhttp;
    }

    getAll(params: any): Observable<PagedResultDtoOfOrganizationUnits> {
        let url_ = "/api/services/app/OrganizationUnits/GetPagedOrganizationUnitssAsync";
        return this._commonhttp.get(url_, params).map(data => {
            if (data) {
                return PagedResultDtoOfOrganizationUnits.fromJS(data);
            } else {
                return null;
            }
        });
    }
    /**
     * 获取组织单元结构树
     */
    GetTreesAsync(): Observable<NzTreeNode[]> {
        let url = "/api/services/app/OrganizationUnits/GetTreesAsync";
        return this._commonhttp.get(url).map(data => {
            let arry = [];
            data = Array.from(data);
            data.map(d => {
                let tree = new NzTreeNode(d);
                arry.push(tree);
            });
            return arry;
        });
    }
    /**
     * 移动组织单位
     * @param input (optional) 
     * @return Success
     */
    moveOrganizationUnit(input: OrganizationUnits | undefined): Observable<OrganizationUnits> {
        let _url = "/api/services/app/OrganizationUnits/MoveOrganizationUnit";
        _url = _url.replace(/[?&]$/, "");

        return this._commonhttp.post(_url, input).pipe(map(data => {
            return data;
        }))
    }
    /**
     * 新增组织单位
     * @param input (optional) 
     * @return Success
     */
    createOrganizationUnit(input: OrganizationUnits | undefined): Observable<OrganizationUnits> {
        let _url = "/api/services/app/OrganizationUnits/CreateOrganizationUnit";
        _url = _url.replace(/[?&]$/, "");

        return this._commonhttp.post(_url, input).pipe(map(data => {
            return data;
        }))
    }
    /**
     * 编辑组织单位
     * @param input (optional) 
     * @return Success
     */
    updateOrganizationUnit(input: OrganizationUnits | undefined): Observable<OrganizationUnits> {
        let _url = "/api/services/app/OrganizationUnits/UpdateOrganizationUnit";
        _url = _url.replace(/[?&]$/, "");

        return this._commonhttp.put(_url, input).pipe(map(data => {
            return data;
        }))
    }
    /**
     * 获取单条数据
     * @param id 
     */
    GetByIdAsync(id: number): Observable<OrganizationUnits> {
        let url = "/api/services/app/OrganizationUnits/GetByIdAsync";
        let param = { 'id': id };
        return this._commonhttp.get(url, param).pipe(map(data => {
            return OrganizationUnits.fromJS(data);
        }));
    }
    /**
     * 删除组织单位
     * @param id 
     */
    DeleteOrganizationUnit(id: number): Observable<any> {
        let url = "/api/services/app/OrganizationUnits/DeleteOrganizationUnit";
        let param = { 'id': id };
        return this._commonhttp.delete(url, param);
    }
    /**
     * 获取组织单位用户列表
     * @param params 
     */
    GetOrganizationUnitUsers(params: any): Observable<PagedResultDto> {
        let url_ = "/api/services/app/OrganizationUnits/GetOrganizationUnitUsers";
        return this._commonhttp.get(url_, params).pipe(map(data => {
            const result = new PagedResultDto();
            result.items = data.items;
            result.totalCount = data.totalCount;
            return result;
        }));
    }
    /**
     * 获取组织单位权限列表
     * @param params 
     */
    GetOrganizationUnitRoles(params: any): Observable<PagedResultDto> {
        let url_ = "/api/services/app/OrganizationUnits/GetOrganizationUnitRoles";
        return this._commonhttp.get(url_, params).pipe(map(data => {
            const result = new PagedResultDto();
            result.items = data.items;
            result.totalCount = data.totalCount;
            return result;
        }));
    }
    /**
     * 获取节点下未添加的成员
     * @param params 
     */
    findUsers(params: any): Observable<PagedResultDto> {
        let _url = "/api/services/app/OrganizationUnits/FindUsers";
        _url = _url.replace(/[?&]$/, "");

        return this._commonhttp.post(_url, params).pipe(map(data => {
            return data;
        }))
    }
    /**
     * 获取节点下未添加的角色
     * @param params 
     */
    findRoles(params: any): Observable<PagedResultDto> {
        let _url = "/api/services/app/OrganizationUnits/FindRoles";
        _url = _url.replace(/[?&]$/, "");

        return this._commonhttp.post(_url, params).pipe(map(data => {
            return data;
        }))
    }
    /**
     * 添加用户到组织单位
     * @param organizationUnitId 
     * @param userIds 
     */
    addUsersToOrganizationUnit(organizationUnitId: number, userIds: Array<number>): Observable<any> {
        let _url = "/api/services/app/OrganizationUnits/AddUsersToOrganizationUnit";
        _url = _url.replace(/[?&]$/, "");

        let params: any = {};
        params.organizationUnitId = organizationUnitId;
        params.userIds = userIds;

        return this._commonhttp.post(_url, params).pipe()
    }
    /**
     * 添加角色到组织单位
     * @param organizationUnitId 
     * @param roleIds 
     */
    addRolesToOrganizationUnit(organizationUnitId: number, roleIds: Array<number>): Observable<any> {
        let _url = "/api/services/app/OrganizationUnits/AddRolesToOrganizationUnit";
        _url = _url.replace(/[?&]$/, "");

        let params: any = {};
        params.organizationUnitId = organizationUnitId;
        params.roleIds = roleIds;

        return this._commonhttp.post(_url, params).pipe()
    }
    /**
     * 删除组织单位关联的用户
     * @param UserId 用户ID
     * @param OrganizationUnitId 组织单位ID
     */
    removeUserFromOrganizationUnit(UserId: number, OrganizationUnitId: number): Observable<any> {
        let _url = "/api/services/app/OrganizationUnits/RemoveUserFromOrganizationUnit";
        let param = { UserId: UserId, OrganizationUnitId: OrganizationUnitId };
        return this._commonhttp.delete(_url, param);
    }
    /**
     * 删除组织单位关联的角色
     * @param RoleId 用户ID
     * @param OrganizationUnitId 组织单位ID
     */
    removeRoleFromOrganizationUnit(RoleId: number, OrganizationUnitId: number): Observable<any> {
        let _url = "/api/services/app/OrganizationUnits/RemoveRoleFromOrganizationUnit";
        let param = { RoleId: RoleId, OrganizationUnitId: OrganizationUnitId };
        return this._commonhttp.delete(_url, param);
    }

}

export class PagedResultDtoOfOrganizationUnits implements IPagedResultDtoOfOrganizationUnits {
    totalCount: number;
    items: OrganizationUnits[];

    constructor(data?: IPagedResultDtoOfOrganizationUnits) {
        if (data) {
            for (var property in data) {
                if (data.hasOwnProperty(property))
                    (<any>this)[property] = (<any>data)[property];
            }
        }
    }

    init(data?: any) {
        if (data) {
            this.totalCount = data["totalCount"];
            if (data["items"] && data["items"].constructor === Array) {
                this.items = [];
                for (let item of data["items"])
                    this.items.push(OrganizationUnits.fromJS(item));
            }
        }
    }

    static fromJS(data: any): PagedResultDtoOfOrganizationUnits {
        let result = new PagedResultDtoOfOrganizationUnits();
        result.init(data);
        return result;
    }

    toJSON(data?: any) {
        data = typeof data === 'object' ? data : {};
        data["totalCount"] = this.totalCount;
        if (this.items && this.items.constructor === Array) {
            data["items"] = [];
            for (let item of this.items)
                data["items"].push(item.toJSON());
        }
        return data;
    }

    clone() {
        const json = this.toJSON();
        let result = new PagedResultDtoOfOrganizationUnits();
        result.init(json);
        return result;
    }
}

export interface IPagedResultDtoOfOrganizationUnits {
    totalCount: number;
    items: OrganizationUnits[];
}